<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">

          <a-col
            :lg="7"
            :md="7"
            :sm="12"
          >
            <a-form-item label="批次号">
              <a-input
                placeholder="请输入批次号"
                v-model="queryParam.batchNo"
              />
            </a-form-item>
          </a-col>
          <a-col
            :lg="7"
            :md="7"
            :sm="12"
          >
            <a-form-item label="保费总金额">
              <a-input
                placeholder="请输入保费总金额"
                v-model="queryParam.totalInsurancePremium"
              />
            </a-form-item>
          </a-col>
          <a-col
            :lg="7"
            :md="7"
            :sm="12"
          >
            <a-form-item label="保单号">
              <a-input
                placeholder="请输入保单号"
                v-model="queryParam.lossPolicy"
              />
            </a-form-item>
          </a-col>
          <a-col
            :lg="3"
            :md="3"
            :sm="12"
          >
            <span class="table-page-search-submitButtons">
              <a-button
                type="primary"
                @click="$refs.table.refresh(true)"
              >查询</a-button>
              <a-button
                style="margin-left: 8px"
                @click="() => queryParam = {}"
              >重置</a-button>
              <span
                style="margin-left:10px"
                @click="upDownFlag=!upDownFlag"
              >
                <a-icon :type="upDownFlag?'up':'down'"></a-icon>
              </span>
            </span>
          </a-col>
          <a-col
            :lg="7"
            :md="7"
            :sm="12"
            v-show="upDownFlag"
          >
            <a-form-item label="报表生成时间">
              <a-date-picker
                valueFormat="YYYY-MM-DD"
                v-model="queryParam.reportGenerationTime"
                style="width: 100%"
                placeholder="请输入报表生成时间"
              />
            </a-form-item>
          </a-col>
          <a-col
            :lg="7"
            :md="7"
            :sm="12"
            v-show="upDownFlag"
          >
            <a-form-item label="保单生效时间">
              <a-date-picker
                valueFormat="YYYY-MM-DD"
                v-model="queryParam.insuranceStartdate"
                style="width: 100%"
                placeholder="请输入保单生效时间"
              />
            </a-form-item>
          </a-col>
          <a-col
            :lg="7"
            :md="7"
            :sm="12"
            v-show="upDownFlag"
          >
            <a-form-item label="支付状态">
              <a-select v-model="queryParam.payStatus">
                <a-select-option value="3">未支付</a-select-option>
                <a-select-option value="4">已支付</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col
            :lg="7"
            :md="7"
            :sm="12"
            v-show="upDownFlag"
          >
            <a-form-item label="支付时间">
              <a-date-picker
                valueFormat="YYYY-MM-DD"
                v-model="queryParam.payTime"
                style="width: 100%"
                placeholder="请输入支付时间"
              />
            </a-form-item>
          </a-col>

        </a-row>
        <a-row>
          <a-col
            :md="10"
            :sm="24"
          >
            <span class="table-page-search-submitButtons">
              <a-button
                style="margin-left: 8px"
                type="primary"
                icon="export"
                @click="exportExcel()"
                :disabled="selectedRowKeys<=0"
              >导出</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <s-table
      size="default"
      ref="table"
      rowKey="id"
      :columns="columns"
      :data="loadData"
      :scroll="{x:1130}"
      :rowSelection="rowSelection"
    >
      <span
        slot="action"
        slot-scope="text, record"
      >
        <a
          v-if="editEnabel"
          @click="handleEdit(record)"
        >编辑</a>
        <a-divider type="vertical" />
        <a
          v-if="removeEnable"
          @click="delByIds([record.id])"
        >删除</a>
      </span>
      <span
        slot="serial"
        slot-scope="text, record, index"
      >
        {{ index + 1 }}
      </span>
      <span
        slot="policyNo"
        slot-scope="text,record"
      >
        <a-input
          placeholder="请输入"
          :value="text"
          @blur="(e)=>policyNoInputChange(e.target.value,record)"
        />
      </span>
      <span
        slot="effectiveTime"
        slot-scope="text,record"
      >
        <a-date-picker
          placeholder="保单生效时间"
          :value="text"
          @change="(date, dateString)=>effectiveTimeChange(dateString,record) "
        />
      </span>
      <span
        slot="serial"
        slot-scope="text, record, index"
      >
        {{ index + 1 }}
      </span>
    </s-table>
    <sysInsuranceDetails-modal
      ref="modal"
      @ok="handleOk"
    />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { getSysInsuranceDetailsList, delSysInsuranceDetails, updateLossPolicyAndInsuranceStartdate } from '@/api/system'
import SysInsuranceDetailsModal from './modules/SysInsuranceDetailsModal2.vue'
import { checkPermission } from '@/utils/permissions'
import { exportExcel } from '@/utils/download'
export default {
  name: 'TableList',
  components: {
    STable,
    SysInsuranceDetailsModal
  },
  data () {
    return {
      upDownFlag: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: this.$form.createForm(this),
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '序号',
          width: 80,
          fixed: 'left',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '报表生成时间',
          width: 200,
          dataIndex: 'reportGenerationTime'
        },
        {
          title: '批次号',
          width: 200,
          dataIndex: 'batchNo'
        },
        {
          title: '订单数量',
          width: 150,
          dataIndex: 'orderNum'
        },
        {
          title: '保费总金额',
          width: 200,
          dataIndex: 'totalInsurancePremium'
        },
        {
          title: '保单号',
          width: 150,
          dataIndex: 'lossPolicy'
        },
        {
          title: '保单生效时间',
          width: 200,
          dataIndex: 'insuranceStartdate'
        },
        {
          title: '支付状态',
          dataIndex: 'payStatus',
          width: 100,
          // eslint-disable-next-line eqeqeq
          customRender: (text) => text == 3 ? '未支付' : '已支付'
        },
        {
          title: '支付时间',
          width: 200,
          dataIndex: 'payTime'
        },
        {
          title: '操作',
          width: 150,
          fixed: 'right',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getSysInsuranceDetailsList(Object.assign(parameter, this.queryParam))
      },
      selectedRowKeys: [],
      selectedRows: [],
      addEnable: checkPermission('system:sysInsuranceDetails:add'),
      editEnabel: checkPermission('system:sysInsuranceDetails:edit'),
      removeEnable: checkPermission('system:sysInsuranceDetails:remove')
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleAdd () {
      this.$refs.modal.add()
    },
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh(true)
      console.log('handleSaveOk')
    },
    delByIds (ids) {
      delSysInsuranceDetails({ ids: ids.join(',') }).then(res => {
        if (res.code === 0) {
          this.$message.success('删除成功')
          this.handleOk()
        } else {
          this.$message.error(res.msg)
        }
        this.selectedRowKeys = []
      })
    },
    exportExcel () {
      exportExcel('/system/sysInsuranceDetails/export', { ids: this.selectedRowKeys.join(',') })
    },
    downLoad (record) {
      exportExcel('/system/sysInsuranceDetails/export', { ids: record.id + '' })
    },
    policyNoInputChange (val, record) {
      const { insuranceStartdate = '', id } = record
      updateLossPolicyAndInsuranceStartdate({
        insuranceStartdate: insuranceStartdate || '',
        id,
        lossPolicy: val
      }).then(res => {
        if (res.code === 0) {
          this.handleOk()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    effectiveTimeChange (val, record) {
      const { lossPolicy = '', id } = record
      updateLossPolicyAndInsuranceStartdate({
        insuranceStartdate: val,
        id,
        lossPolicy: lossPolicy || ''
      }).then(res => {
        if (res.code === 0) {
          this.handleOk()
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
<style lang="less" scoped>
/deep/ .table-page-search-wrapper .ant-form-inline .ant-form-item .ant-form-item-control {
  width: 180px;
}
</style>
